package com.speedy.course.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.speedy.course.databinding.FragmentWeekBinding
import com.speedy.course.databinding.ItemLessonBinding
import com.speedy.course.db.entity.Lesson
import com.speedy.course.vm.WeekViewModel
import kotlinx.coroutines.launch

class WeekFragment : Fragment() {

    private var _binding: FragmentWeekBinding? = null

    private val binding get() = _binding!!

    private val viewModel: WeekViewModel by viewModels()

    private var lessonAdapter = LessonAdapter()

    private var weekDay:Int = 0

    companion object{
        fun newInstance(weekDay:Int):WeekFragment{
            return WeekFragment().apply {
                arguments =  Bundle().apply {
                    putInt("weekDay",weekDay)
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = FragmentWeekBinding.inflate(inflater, container, false)

        initView()
        initObserver()

        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        lifecycleScope.launch {
            viewModel.queryLessonList()
        }

    }

    private fun initObserver() {
        lifecycleScope.launch {
            viewModel.lessonListStateFlow.collect{
                lessonAdapter.clear()
                lessonAdapter.setData(it)
                lessonAdapter.notifyDataSetChanged()
            }
        }
    }


    private fun initView() {

        _binding?.apply {

            lessonAdapter = LessonAdapter()

            recyclerView.adapter = lessonAdapter

        }

    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}



private class LessonAdapter:RecyclerView.Adapter<LessonViewHolder>(){

    private val data = mutableListOf<Lesson>()

    fun setData(list: List<Lesson>?){
        if (list != null) {
            data.addAll(list)
        }
    }

    fun clear(){
        data.clear()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LessonViewHolder {
        val binding = ItemLessonBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return LessonViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: LessonViewHolder, position: Int) {
        holder.binding.lesson = data[position]
    }

}


private class LessonViewHolder(val binding: ItemLessonBinding) :RecyclerView.ViewHolder(binding.root){

}